package com.cwt_taxi_xa;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.cwt_taxi_xa.protocol.Decoder;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.protocol.voice.ViewManager;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;

/**
 * 声耕升级测试
 *
 */
public class ActivityVoice extends CWTBaseActivity implements View.OnClickListener {

	final static String TAG = "ActivityVoice";

	Spinner spEvaluation;

	EditText etFilePath;

	SeekBar sbAcoustics;

	Button btnStartUpdate,btnUpdateBuffer,btnFtpTest;

	TextView mTextView;

	final String[] violationArray = {"无故拒载","故意绕路","强行拼车","不打表","中断服务","不按标准收费","未按规范标准服务","乘客投诉","紧急报警"};
	ViewManager viewManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		openSwipeBack(true);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_voice);
		AppLog.d(TAG, "onCreate");
		viewManager = new ViewManager(this);
		spEvaluation = (Spinner)findViewById(R.id.sp_voice_evaluation);

		sbAcoustics = (SeekBar)findViewById(R.id.sb_volume_voice);
        mTextView  = (TextView) findViewById(R.id.tv_acoustics_version);
		etFilePath = (EditText) findViewById(R.id.et_file_path);
		btnStartUpdate = (Button)findViewById(R.id.btn_start_update);
		btnUpdateBuffer = (Button)findViewById(R.id.btn_update_buffer);
		btnFtpTest = (Button)findViewById(R.id.btn_ftp_test);
		findViewById(R.id.btn_acoustics_power_on).setOnClickListener(this);
		findViewById(R.id.btn_acoustics_power_off).setOnClickListener(this);
		setFilePath();
		btnStartUpdate.setOnClickListener(this);
		btnUpdateBuffer.setOnClickListener(this);
		btnFtpTest.setOnClickListener(this);
		//自定义选择填充后的字体样式
		//只能是textview样式，否则报错：ArrayAdapter requires the resource ID to be a TextView
		ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, violationArray);
		spEvaluation.setAdapter(spinnerAdapter);
		spEvaluation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				try {
					//MCUUtil.mVoiceTool.dispatchViolation(position);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});
	}

	public void setFilePath(){
		etFilePath.setText("/mnt/sdcard/SG-SoftV100HardV110.bin");
		int softwareVER = MCUUtil.mVoiceTool.focalAcousticsUpgrade.getSoftwareVER();
		int hardwareVER = MCUUtil.mVoiceTool.focalAcousticsUpgrade.getHardwareVER();
        mTextView.setText("硬件版本:" + hardwareVER + ";软件版本:" + softwareVER);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AppLog.d(TAG,"onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	public void setStatus(int index, boolean status) {

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"sendMsg type:" + type);
	}


	@Override
	public void onClick(View v) {

        try {
            switch (v.getId()){
                case R.id.btn_start_update:
					MCUUtil.mVoiceTool.closeTimer();
					MCUUtil.mVoiceTool.focalAcousticsUpgrade.setFilePath(etFilePath.getText().toString());
                    //JniInterface.fwPathToJNI(etFilePath.getText().toString(),true);
                    break;
                case R.id.btn_update_buffer:
                    MCUUtil.mVoiceTool.updateFrame();
                    break;
				case R.id.btn_ftp_test:
					String dataString = "7E8BF400551010047515540EA0006453472D536F6674563230304861726456313130006561737969747300656173796974730008493131332E3134302E37312E3235320053472D536F66745632303048617264563131302E62696E0000650038214EF37E";
					Decoder.decoderQH808Data(Tools.parseHexStr2Byte(dataString),MainApplication.getInstance());
					//viewManager.showFloatBall();
					break;
				case R.id.btn_acoustics_power_off:
					MCUUtil.setIO(2,false);
					break;
				case R.id.btn_acoustics_power_on:
					MCUUtil.setIO(2,true);
					break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
